package com.intelligentcar.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.intelligentcar.springboot.bean.Advertisement;

import com.intelligentcar.springboot.bean.City;
import com.intelligentcar.springboot.bean.Partner;
import com.intelligentcar.springboot.service.AdvertisementService;
import org.aspectj.weaver.ast.Test;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RestController
public class AdvertisementController {
    public String AdvertPicutre;
    @Autowired
    AdvertisementService advertisementService;
    @Autowired
    HttpServletRequest request;
    //可上传文件类型定义
    private List fileTypes = new ArrayList() {
        {
            add("jpg");
            add("png");
        }
    };

    @Value("${server.port}")
    private String servePrort;

    @RequestMapping("upload2")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file) throws FileNotFoundException {
        String IP = "1";
//        System.out.println("端口号：" + servePrort);
        try {
            /*获取IP*/
            IP = InetAddress.getLocalHost().getHostAddress();
            System.out.println("IP是" + IP);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String pathStr = Test.class.getResource("/").getPath();
//        System.out.println("1:" + pathStr);
        String pathSbu = pathStr.substring(1);/*前面有个"/"要去掉*/
        ;
//        System.out.println("2:" + pathSbu);
//         String pathSbu = ResourceUtils.getURL("classpath:").getPath();
        //判断文件夹是否存在，如果不存在则进行创建

        JSONObject jsonRsult = new JSONObject();
        if (file.isEmpty()) {
            jsonRsult.put("code", 201);
            jsonRsult.put("msg", "请选择文件");
            return jsonRsult.toJSONString();
        }
        String originalFilename = file.getOriginalFilename();
        AdvertPicutre = "http://" + IP + ":" + servePrort + "/vending/" + originalFilename;
//        System.out.println(AdvertPicutre);
        String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
        if (!fileTypes.contains(fileType)) {
            jsonRsult.put("code", 201);
            jsonRsult.put("msg", "仅支持 .jpg .png 文件上传");
            return jsonRsult.toJSONString();
        }
        String path = pathSbu + "/static/vending/";

        Path filePath = Paths.get(path + originalFilename);
        try {

            byte[] fileBytes = file.getBytes();
            Files.write(filePath, fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        jsonRsult.put("code", 0);
        jsonRsult.put("msg", "上传成功");
        return jsonRsult.toJSONString();
    }

    //    查询新闻公告界面数据
    @RequestMapping("/Lookadvertisement1")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list(@RequestBody Advertisement advertisement) {
        Map map = new HashMap();
        if (advertisement.getInput().equals("") || advertisement.getInput()==""){
            List<Advertisement> list = advertisementService.LookAdvertisement();
            map.put("list", list);
            map.put("total", advertisementService.selectAdvertisement());
        }

        for(int i=advertisement.getInput().length();--i>=0;) {
            int chr = advertisement.getInput().charAt(i);
            if (chr < 48 || chr > 57) {
//                System.out.println(advertisement.getInput()+"11111");
                List<Advertisement> list = advertisementService.Lookpname(advertisement);
                map.put("list", list);
                map.put("total", advertisementService.selectAdvertisement());
            } else {
//                System.out.println("22222222");
//                System.out.println(advertisement.getInput());
                List<Advertisement> list = advertisementService.LookaddTime(advertisement.getInput());
                map.put("list", list);
                map.put("total", advertisementService.selectAdvertisement());
            }
        }
        return map;
    }


    //    新闻公告界面数据展示
    @RequestMapping("/Lookadvertisement")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list() {
        List<Advertisement> list = advertisementService.LookAdvertisement();
        Map map = new HashMap();
        map.put("list", list);
        map.put("total", advertisementService.selectAdvertisement());
        return map;
    }

    //    下拉框获取所有广告商
    @RequestMapping("/Lookadvertisement11")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list11() {
        List<Partner> list = advertisementService.LookAdvertisement11();
        Map map = new HashMap();
        map.put("list", list);
//        System.out.println(list);
        return map;
    }


    //    广告界面中的修改信息操作
    @RequestMapping("/updadvertisement1")
    @ResponseBody //跳过视图解析器,直接返回数据
    public int updateAdvertisement(@RequestBody Advertisement advertisement) {

        System.out.println(advertisement.getAdid() + "," +  advertisement.getAdvertPicutre()+ "," + advertisement.getAdvertInfo()+ "," +
                advertisement.getAdlocation()+ "," + advertisement.getAddTime());

        int a = advertisementService.updateAdvertisement(advertisement.getAdid(), AdvertPicutre,
                advertisement.getAdvertInfo(), advertisement.getAdlocation(),advertisement.getAddTime());

        return a;
    }

    //
    //广告界面中的删除信息操作
    @RequestMapping("/deladvertisement1")//请求路径
    @ResponseBody //跳过视图解析器,直接返回数据
    public String deleteAdvertisement(long id) {
//        String id1 =intValue(id);;
        advertisementService.deleteAdvertisement(id);
//        System.out.println("id是:"+id);
        return "success";
    }

    //
    //广告界面中的新增信息操作
    @RequestMapping("/addAdvertisement")
    @ResponseBody //跳过视图解析器,直接返回数据
    public String addAdvertisement(@RequestBody Advertisement advertisement) {
        int a = advertisementService.addAdvertisement(advertisement.getAdid(),AdvertPicutre,
                advertisement.getAdvertInfo(), advertisement.getAddTime(),advertisement.getAdlocation());
        System.out.println(a+"a的值");
        if(a>0){
            return  "success";
        }else {
            return "failure";
        }
    }
}

